/**
 * create by young (young2f@outlook.com) on 2024/4/21
 */
'use strict';
const base = require('../base/controllerBase');

class UserController extends base {
  async register() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        userType: { type: 'string' },
        username: { type: 'string' },
        password: { type: 'string' },
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);
      this.app.logger.info('user register', body);
      const res = await service.userService.register(body);
      ctx.body = res;
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      this.app.logger.error('user register', _message, e);
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async login() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { username: { type: 'string' }, password: { type: 'string' } };
      console.log('login');
      ctx.validate(model, body);

      const res = await service.userService.login(body);
      body.action = 'login';
      body.status = res.status;
      const tc = res.result ? res.result.tenantCode : undefined;
      body.tenantCode = tc;
      ctx.body = res;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async resetPassword() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        username: { type: 'string' },
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);

      const res = await service.userService.resetPassword(body);

      ctx.body = res;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findUser() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;

      const model = { username: { type: 'string' }, tenantCode: { type: 'string' } };
      ctx.validate(model, body);
      const result = await ctx.model.User.findOne(body, null, { lean: true });
      result && delete result['password'];

      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findUsers() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { tenantCode: { type: 'string' } };
      ctx.validate(model, body);
      const result = await ctx.app.dbs.find('User', body);
      result && delete result['password'];

      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findUsersByPage() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        skip: { type: 'number' },
        limit: { type: 'number' },
      };
      ctx.validate(model, body);
      const result = await service.userService.findUsersByPage(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateUser() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { username: { type: 'string' }, tenantCode: { type: 'string' } };
      ctx.validate(model, body);
      const res = await service.userService.updateUser(body);
      ctx.body = res;
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteUser() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { userCode: { type: 'string' }, tenantCode: { type: 'string' } };
      ctx.validate(model, body);
      const res = await service.userService.deleteUser(body);
      ctx.body = res;
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updatePassword() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      console.log('update pass', body);
      const model = { password: { type: 'string' }, oldPassword: { type: 'string' }, tenantCode: { type: 'string' } };
      ctx.validate(model, body);
      const res = await service.userService.updatePassword(body);
      ctx.body = res;
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }



}

module.exports = UserController;
